package com.spirit.converter;

import com.spirit.domain.user.Aderess;
import com.spirit.domain.user.User;
import org.apache.commons.lang.StringUtils;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

/**
 * 自定义转换器
 */
@Component
public class UserConverter implements Converter<String,User>{

    @Override
    public User convert(String source) {
        //张三-123456-22-0-zhangsan@163.com-学府街-太原
        User user = new User();
        if(StringUtils.isNotEmpty(source)) {
            String[] strings = source.split("-");
            user.setUserName(strings[0]);
            user.setPassWord(strings[1]);
            user.setUserAge(StringUtils.isNotEmpty(strings[2]) ? Integer.parseInt(strings[2]) : 0);
            user.setGender(StringUtils.isNotEmpty(strings[3]) ? "1".equals(strings[3]) ? strings[3] : "2" : "2");
            user.setEmail(strings[4]);
            user.setAderess(new Aderess(strings[5],strings[6],"山西",5));
        }
        return user;
    }
}
